package nkr1pt.popkorn.download;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Kristof Vanhaeren
 */
public class RapidshareValidator extends DownloadCandidateValidator {

    private static final Logger logger = Logger.getLogger(RapidshareValidator.class.getName());

    @Override
    public boolean doHostSpecificValidation(DownloadCandidate downloadCandidate) {
        logger.log(Level.INFO, "Checking if all links for {0} are alive.", downloadCandidate.getName());
        try {
            for (DownloadCandidateItem item : downloadCandidate.getItems()) {
                HttpURLConnection urlc = (HttpURLConnection) item.getUrl().openConnection();
                if (!StringUtils.isEmpty(item.getCookie())) {
//                    urlc.setRequestMethod("POST");
                    urlc.setRequestProperty("Cookie", item.getCookie());
//                    urlc.setDoOutput(true);
//                    urlc.setDoInput(true);
//                    urlc.setUseCaches(false);
//                    urlc.setAllowUserInteraction(false);
//                    urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
//
//                    OutputStream out = urlc.getOutputStream();
//                    Writer writer = new OutputStreamWriter(out, "UTF-8");
//                    writer.write("dl.start=PREMIUM");
//                    writer.flush();
//                    writer.close();
//                    out.close();

                }
                String alive = "OK";
                if (urlc.getContentType().contains("text")) {
                    alive = "NOK";
                    logger.log(Level.INFO, "Content of failed validation was:\n ");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        logger.log(Level.INFO, line);
                    }
                    reader.close();
                }

                logger.log(Level.INFO, "Checking {0}... {1}", new Object[]{item.getName(), alive});
                if (alive.equals("NOK")) {
                    logger.log(Level.INFO, "{0} not added to the downloading queue, because 1 or more links are dead.", new Object[]{downloadCandidate.getName()});
                    downloadCandidate.setState(DownloadCandidateState.INVALID);
                    return false;
                }

                item.setSize(Long.valueOf(urlc.getContentLength()));
                if (item.getSize() == item.getDownloaded()) {
                    item.setState(DownloadCandidateState.COMPLETE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            downloadCandidate.setState(DownloadCandidateState.INVALID);
            return false;
        }

        logger.log(Level.INFO, "Validation for {0} completed, looking good.", downloadCandidate.getName());
        return true;
    }

}